package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	s := "Yes我爱慕课网!" // UTF-8,一个字符占字节是可变的，英文1byte，中文3byte
	fmt.Println(s)

	for _, b := range []byte(s) { //获取字节数组，s占19byte，9rune
		fmt.Printf("%X ", b) //%X  打印16进制
	}
	fmt.Println()

	for i, ch := range s { // ch is a rune
		//fmt.Printf("(%d %c) ", i, ch)//%c 打印字符
		fmt.Printf("(%d %X) ", i, ch) //i字节开始索引
	}
	fmt.Println()

	fmt.Println("Rune count:",
		utf8.RuneCountInString(s))

	bytes := []byte(s)
	for len(bytes) > 0 {
		ch, size := utf8.DecodeRune(bytes) //utf-8字节数组转字符，一个一个转
		bytes = bytes[size:]
		fmt.Printf("%c ", ch)
	}
	fmt.Println()

	for i, ch := range []rune(s) { //字符串转字符数组s，支持中英文的字符
		fmt.Printf("(%d %c) ", i, ch)
	}
	fmt.Println()
}
